package org.tacademy.myanother;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MyAnotherActivity extends Activity {

	public static final int REQUEST_NAME = 100;
	public static final int REQUEST_NUM = 200;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        Button button01 = (Button)findViewById(R.id.button01);
        button01.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				callActivity(REQUEST_NAME);
				
			}
		});
        
        Button button02 = (Button)findViewById(R.id.button02);
        button02.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				callActivity(REQUEST_NUM);
			}
		});
   //     Toast.makeText(getApplicationContext(), "onCreate()", 1000).show();
    }
	
	@Override
	protected void onDestroy() {
		clearState();
	//	Toast.makeText(getApplicationContext(), "onDestroy()", 1000).show();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		saveCurrentState();
	//	Toast.makeText(getApplicationContext(), "onPause()", 1000).show();
		super.onPause();
	}

	@Override
	protected void onRestart() {
	//	Toast.makeText(getApplicationContext(), "onRestart()", 1000).show();
		super.onRestart();
	}

	@Override
	protected void onResume() {
		restoreState();
	//	Toast.makeText(getApplicationContext(), "onResume()", 1000).show();
		super.onResume();
	}

	@Override
	protected void onStart() {
	//	Toast.makeText(getApplicationContext(), "onStart()", 1000).show();
		super.onStart();
	}

	@Override
	protected void onStop() {
	//	Toast.makeText(getApplicationContext(), "onStop()", 1000).show();
		super.onStop();
	}
	
	private void callActivity(int requestCode){
		if(requestCode == REQUEST_NAME){
			Intent intent = new Intent(this,NameActivity.class);
			intent.putExtra("no", "123");
			startActivityForResult(intent, requestCode);
		}
		else if(requestCode == REQUEST_NUM){
			Intent intent = new Intent(this,NumberActivity.class);
			intent.putExtra("no", "456");
			startActivityForResult(intent, requestCode);
		}
	}
	
	
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
	//	super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == REQUEST_NAME){
			if(resultCode == 1){
				String name = data.getStringExtra("name");
				Toast.makeText(getApplicationContext(), "data:"+name+" requestCode:"+ requestCode
						+"resultCode:"+resultCode, Toast.LENGTH_LONG).show();
			}
		}
		else if(requestCode == REQUEST_NUM){
			if(resultCode == 2){
				String number = data.getStringExtra("number");
				Toast.makeText(getApplicationContext(), "data:"+number+"requestCode:"+requestCode
						+"resultCode:"+resultCode, Toast.LENGTH_LONG).show();
			}
		}
		
	}
	
	private void saveCurrentState(){
		SharedPreferences myPref = getSharedPreferences("preference01", Activity.MODE_PRIVATE);
		SharedPreferences.Editor myEditor = myPref.edit();
		myEditor.putString("message", "hello");
		myEditor.commit();
	}
	
	private void restoreState(){
		SharedPreferences myPref = getSharedPreferences("preference01", Activity.MODE_PRIVATE);
		if((myPref != null) && myPref.contains("message")){
			String data = myPref.getString("message", "");
			Toast.makeText(getApplicationContext(), data, 1000).show();
		}
	}
	
	private void clearState(){
		SharedPreferences myPref = getSharedPreferences("preference01", Activity.MODE_PRIVATE);
		SharedPreferences.Editor myEditor = myPref.edit();
		myEditor.clear();
		myEditor.commit();
	}
	
}















